//
//  SEGYellowPagesTypeViewController.m
//  ExcellentHome
//
//  Created by liangxinqiang on 14-11-22.
//  Copyright (c) 2014年 陈区. All rights reserved.
//


#import "SEGYellowPagesTypeViewController.h"
#import "YellowPagesModel.h"

@interface SEGYellowPagesTypeViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, copy)  NSString *callString;
/// tableView
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation SEGYellowPagesTypeViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self showCustomNavigationBackButton];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.typeAry.count;
}
- (CGFloat)tableView:(UITableView *) tableView heightForRowAtIndexPath:(NSIndexPath *) indexPath {
    return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
        UILabel *lbName = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, tableView.frame.size.width, 44)];
        lbName.textColor = kColor_2;
        lbName.backgroundColor = [UIColor clearColor];
        lbName.font = [UIFont systemFontOfSize:FONT_LIST_AND_DETAIL_SIZE];
        lbName.tag = 1001;
        [cell.contentView addSubview:lbName];
        UIImage *image = [[UIImage imageWithContentsOfFile:SEGPicName(@"home_numberbook_btn_green")] imageByTintColor:kColor_T];
        CustomButtonView *btnStop = [CustomButtonView buttonWithType:UIButtonTypeCustom];
        btnStop.userInteractionEnabled = NO;
        btnStop.frame = CGRectMake(CGRectGetWidth(self.view.bounds)-20 - 25,8.5,25,25);
        [btnStop setTitleColor:kColor_W forState:UIControlStateNormal];
        btnStop.titleLabel.font = [UIFont systemFontOfSize:12];
        [btnStop setBackgroundImage:image forState:UIControlStateNormal];
        [cell.contentView addSubview:btnStop];

        if (indexPath.row == self.typeAry.count-1) {
            UIImageView *linebg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 43, CGRectGetWidth(self.view.bounds), 1)];
            linebg.image = [UIImage imageWithContentsOfFile:SEGPicName(@"line_460")];
            [cell.contentView addSubview:linebg];
        } else {
            UIImageView *linebg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 43, CGRectGetWidth(self.view.bounds), 1)];
            linebg.image = [UIImage imageWithContentsOfFile:SEGPicName(@"line_460")];
            [cell.contentView addSubview:linebg];
        }
    }
    
    UILabel *lbName = (UILabel *)[cell.contentView viewWithTag:1001];
    YellowPagesModel *entity = [self.typeAry objectAtIndex:indexPath.row];
    lbName.text = entity.name;
    
    return cell;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YellowPagesModel *entity = [self.typeAry objectAtIndex:indexPath.row];
    [self showCallAlertWithCallString:entity.tel];
}

#pragma mark - SEGNewAlertView
- (void)showCallAlertWithCallString:(NSString *)callString {
    self.callString = callString;
    SEGNewAlertView *alertView = [[SEGNewAlertView alloc] init];
    alertView.dismissWhenTouch = YES;
    [alertView showWithAlertText:[NSString stringWithFormat:@"%@",self.callString] delegate:self buttonTitlesArray:@[@"取消",@"呼叫"]];
}
#pragma mark - SEGNewAlertViewDelegate
- (void)segNewAlertView:(UIView *_Nullable)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        if (@available(iOS 8.0, *)) {
            NSString *str = [NSString stringWithFormat:@"telprompt://%@",self.callString];
            if (@available(iOS 10.0, *)) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:nil];
            } else {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
            }
        }
    }
}

#pragma mark - Getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = kColor_W;
    }
    return _tableView;
}


@end
